<?php 
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
require('../includes/init.php');
require('./classes/control/control_product_operation.php');
require('./classes/control/control_product.php');
class ProductOperation
{

	var $product_operation_c;
	var $product_c;
	
	function __construct() {
	
		$this->product_operation_c = new ProductOperationControl();
		$this->product_c = new ControlProduct();
	}
	
	//公共产品修改增加
	function index()
	{
		if($_REQUEST['type'])
		{	
			$type = $_REQUEST['type']=='add' ? 'add': 'update';
			$insert = $insert_more = $insert_other = '';	

			if($_POST['sub_from'])
			{
				
				$change_rows  = 0;
				$product_id = $this->product_operation_c->change_product($type,(int)$_POST['product_id'],(int)$_POST['product_cbsid'],
				$_POST['product_sn'],$_POST['product_name'],$_POST['product_version'],$_POST['product_brand'],$_POST['product_platform'],
				$_POST['is_free'],$_POST['is_year_charge'],$_POST['is_trial'],$_POST['product_rel'],$_POST['product_icon_min'],
				$_POST['product_icon_max'],$_POST['product_box_min'],$_POST['product_box_middle'],$_POST['product_box_big'],
				$_POST['product_video_promo'],$_POST['product_ad'],$_POST['product_intro'],$_POST['product_content'],$_POST['product_key_feature'],
				$_POST['product_support_intro'],$_POST['product_support_content'],$_POST['techSpec_content_table'],$_POST['product_guide_content'],
				$_POST['video_url'],trim((int)$_POST['video_width']),trim((int)$_POST['video_higth']),trim($_POST['contrast_table']),trim($_POST['product_variant']));
				
				if($product_id == -1 && $type == 'add')
				{
					echo  '<script type="text/javascript"> alert("数据库已经存在此条记录！"); </script>';
				}
				elseif ($product_id == 0 && $type == 'add')
				{
					echo  '<script type="text/javascript"> alert("增加失败！"); </script>';
				}
				elseif ($product_id == 0 && $type == 'update')
				{
					echo  '<script type="text/javascript"> alert("修改失败！"); </script>';
				}elseif($product_id >0)
				{
					if($type == 'add')
					{	
						if(!empty($product_id))
						{
							for($i=1;$i<=$_POST['feature_start'];$i++)
							{
								if(!empty($_POST['title_feature'.$i]))
								{
									$insert.= "('".$product_id."','".$_POST['title_feature'.$i]."','".$_POST['image_min_feature'.$i]."','".$_POST['image_max_feature'.$i]."',
									'".$_POST['description_feature'.$i]."','0','$i','".$_SERVER['REQUEST_TIME']."','".$_SESSION['AName']."'),";
								}
							}
							
							for($j=1;$j<=$_POST['more_feature_start'];$j++)
							{
								if(!empty($_POST['title_more_feature'.$j]))
								{
									$insert_more.= "('".$product_id."','".$_POST['title_more_feature'.$j]."','".$_POST['image_more_feature'.$j]."',
									'".$_POST['description_more_feature'.$j]."','1','$j','".$_SERVER['REQUEST_TIME']."','".$_SESSION['AName']."'),";
								}
							}
							for($j=1;$j<=$_POST['other_feature_start'];$j++)
							{
								if(!empty($_POST['title_other_feature'.$j]))
								{
									$insert_other .= "('".$product_id."','".$_POST['title_other_feature'.$j]."','".$_POST['image_other_feature'.$j]."',
									'".$_POST['description_other_feature'.$j]."','2','$j','".$_SERVER['REQUEST_TIME']."','".$_SESSION['AName']."'),";
								}
							}
							
							if(!empty($product_id) && empty($insert) && empty($insert_more) && empty($insert_other) )
							{
								$change_rows = 1;
							}
						
						}
					} elseif ($type == 'update') {
						$feature_a = $more_feature_a = $other_feature_a = array();
						
						$feature_a = 	$this->loop_change_feature($_POST['feature_start'] ,$_POST, '');
				
						$insert = $feature_a['insert'];
						$change_rows = $feature_a['rows'];
						
						$more_feature_a = $this->loop_change_feature($_POST['more_feature_start'] ,$_POST,'more_', 1);
						$insert_more = $more_feature_a['insert'];
						$change_rows = $change_rows + $more_feature_a['rows'];
						
						$other_feature_a = $this->loop_change_feature($_POST['other_feature_start'] ,$_POST,'other_', 2);
						$insert_other = $other_feature_a['insert'];
						$change_rows = $change_rows + $more_feature_a['rows'] + $other_feature_a['rows'];
						
						if(!empty($_POST['product_id']) && empty($insert) && empty($insert_more) && empty($insert_other) )
						{
							$change_rows = 1;
						}
					}
				}
				
				if(!empty($insert))
				{
					$insert = substr($insert, 0, strlen($insert) - 1) ;
					$change_rows = $this->product_operation_c->insert_feature_info($insert,$product_id,'insert');//批量插入
				}
				
				if(!empty($insert_more))
				{
					$insert_more = substr($insert_more, 0, strlen($insert_more) - 1) ;
					$change_rows = $this->product_operation_c->insert_feature_info($insert_more,$product_id,'m_insert');//批量插入
				}
				
				if(!empty($insert_other))
				{
					$insert_other = substr($insert_other, 0, strlen($insert_other) - 1) ;
					$change_rows = $this->product_operation_c->insert_feature_info($insert_other,$product_id,'m_insert');//批量插入
				}
				
				if ($type == 'add') {
					if($change_rows>0)
					{
						file_get_contents('http://cms.wondershare.com/wondershare/product.php?action=sync_version_cbs&type=admin&product_cbsid='.(int)$_POST['product_cbsid'].'&product_id='.$product_id);
						file_get_contents('http://cms.wondershare.com/wondershare/product.php?action=sync_price_cbs&type=admin&product_cbsid='.(int)$_POST['product_cbsid'].'&product_id='.$product_id);
						echo  '<script type="text/javascript"> alert("增加成功！"); 
						window.location.href="/wondershare/product.php?action=productList"; </script>';
						
						//清除ws的download页面的产品缓存信息
						$this->product_operation_c->clear_ws_download_cache();
					}
				} elseif ($type == 'update') {
					
					if ($change_rows > 0) {
						//清除ws的download页面的产品缓存信息
						$this->product_operation_c->clear_ws_download_cache();
						
						//定义更新产品模板相关联的 id
						$rows = $this->product_c->getWonProductInfo($product_id);				
						$update_arr = array('id' =>(int) $rows['id'],//won_product表id
	
											'w_type' => brandToWebtype($rows['product_brand']),
											'operation_type' => 'product',
											'location' => '/wondershare/product_operation.php?action=index&type=update&product_id='.$product_id,
						);
						
					 	echo '<script type="text/javascript">
							var ret = window.confirm("修改成功，是否生成其关联页面？\n\n确定：生成，取消：不生成");
							if( ret )
							{
								 window.location.href="/wondershare/template.php?action=updateTemplate&type=pub&'.http_build_query($update_arr).'";
							}else
							{
								 window.location.href="/wondershare/template.php?action=updateTemplate&type=update&'.http_build_query($update_arr).'";
							}
							</script>';
					}
				}
	
			}
			
			if ($type == 'add') {
				$GLOBALS['smarty']->assign('feature',     array(0=>0));
				$GLOBALS['smarty']->assign('m_feature',   array(0=>0));
				$GLOBALS['smarty']->assign('c_feature',   1);
				$GLOBALS['smarty']->assign('c_m_feature', 1);
				$GLOBALS['smarty']->assign('tile',     '添加');
				$GLOBALS['smarty']->assign('form_act',     'add');
		
			} elseif ($type == 'update') {
				
				$product = $feature = $m_feature = array();
				if (!empty($_GET['product_id'])) {
					
					$product = $this->product_operation_c->get_product_info((int)$_GET['product_id']);
					$feature = $this->product_operation_c->get_feature_info((int)$_GET['product_id'], 'feature');
					$m_feature = $this->product_operation_c->get_feature_info((int)$_GET['product_id'], 'm_feature');
					$o_feature = $this->product_operation_c->get_feature_info((int)$_GET['product_id'], 'o_feature');
					$GLOBALS['smarty']->assign('feature',     $feature);
					$GLOBALS['smarty']->assign('m_feature',   $m_feature);
					$GLOBALS['smarty']->assign('o_feature',   $o_feature);
					$GLOBALS['smarty']->assign('c_feature',   count($feature));
					$GLOBALS['smarty']->assign('c_m_feature', count($m_feature));
					$GLOBALS['smarty']->assign('c_o_feature', count($o_feature));
					$GLOBALS['smarty']->assign('product',     $product[0]);
				}
				
				$GLOBALS['smarty']->assign('tile',     '修改');
				$GLOBALS['smarty']->assign('form_act',     'update');
				unset($product);unset($feature);unset($m_feature);
			}
			
			
			$product_id = $_REQUEST['product_id'];
			$GLOBALS['smarty']->assign('brand', strtolower($this->product_operation_c->get_brand($product_id)) );
			$GLOBALS['smarty']->display('product_operation_info.htm');  
		}

	}
	
	function loop_change_feature($length ,$_POST, $prefix = '', $feature_type = 0) {
		
		$insert = '';

		for ($j = 1; $j <= $length; $j++) {
			

			if (!empty($_POST['title_'.$prefix.'feature'.$j]))	{
					
				if(!empty($_POST[$prefix.'feature_id'.$j]))
				{
					if(empty($prefix))
					{
			
						$change_rows = $this->product_operation_c->update_feature_info('feature',(int)$_POST['feature_id'.$j],$_POST['description_feature'.$j],
						$_POST['title_feature'.$j],'',$_POST['image_min_feature'.$j],$_POST['image_max_feature'.$j],(int)$_POST['feature_order'.$j]);		
					}else
					{
					
						$change_rows = $this->product_operation_c->update_feature_info($prefix.'feature',(int)$_POST[$prefix.'feature_id'.$j],$_POST['description_'.$prefix.'feature'.$j],
						$_POST['title_'.$prefix.'feature'.$j],$_POST['image_'.$prefix.'feature'.$j],'','',(int)$_POST['feature_'.$prefix.'order'.$j]);
					}
				}else 
				{
					if(!empty($prefix))
					{
						$insert.= "('".(int)$_POST['product_id']."','".$_POST['title_'.$prefix.'feature'.$j]."','".$_POST['image_'.$prefix.'feature'.$j]."',
						'".$_POST['description_'.$prefix.'feature'.$j]."',$feature_type,'".$_SERVER['REQUEST_TIME']."','".$_SESSION['AName']."','".(int)$_POST['feature_'.$prefix.'order'.$j]."'),";
					}else 
					{
						$insert.= "('".(int)$_POST['product_id']."','".$_POST['title_feature'.$j]."','".$_POST['image_min_feature'.$j]."','".$_POST['image_max_feature'.$j]."',
						'".$_POST['description_feature'.$j]."',$feature_type,'".$_SERVER['REQUEST_TIME']."','".$_SESSION['AName']."','".(int)$_POST['feature_order'.$j]."'),";
					}	
				}
			}else 
			{
				if(!empty($_POST[$prefix.'feature_id'.$j]))
				{
					$change_rows = $this->product_operation_c->update_feature_info('delete',(int)$_POST[$prefix.'feature_id'.$j]);
				}
			}
		}
		return  array('insert'=>$insert,'rows'=>$change_rows);
	}
	
	/**
	* 发布相关联的模板
	*/
	function  pub_tem()
	{
		if(!empty($_GET['product_id']))
		{
		
			$ws_p_id = $GLOBALS['db']->getOne('SELECT id FROM won_product WHERE won_product_id = '.(int)$_GET['product_id']);//得到ws产品ID
			if(!empty($ws_p_id))
			{
				$pub_status = pub_template(26,$ws_p_id,'Field_20044');
				$pub_status = $pub_status + pub_template(45,$ws_p_id,'Field_20350');//到产品单页模板找
				
				if (!empty($pub_status)) {
					echo '<script type="text/javascript">
					window.location.href="http://cms.wondershare.com/wondershare/product_operation.php?action=index&type=update&product_id='.(int)$_GET['product_id'].'"; 
					</script>';
				} else {
					echo '<script type="text/javascript">
					alert("此产品木有模板文件或者发布模板失败");
					window.location.href="http://cms.wondershare.com/wondershare/product_operation.php?action=index&type=update&product_id='.(int)$_GET['product_id'].'"; 
					</script>';
				}
			}
		
		}
	}

}

$action    = $_REQUEST['action']; 
$product_operation   = new ProductOperation;

if (method_exists($product_operation, $action)) {
	$product_operation->$action();
} else {
	$product_operation->index();
}


?>
